/***************************************************************************************************
 *
 * 
 *
 ***************************************************************************************************/
function contacts_load_all(page)
{
	var url = '';
	switch(ITEM_FILTER)
	{
		case '1':
			url = 'index.php?c=ajax_contacts&a=contacts_get_responsed'; break;
		case '2':
			url = 'index.php?c=ajax_contacts&a=contacts_get_all'; break;
		default:
			url = 'index.php?c=ajax_contacts&a=contacts_get_unresponsed'; break;
	}
	url += '&pge=' + page;

	new Ajax.Request(url, {
  	method:'get',
  	onSuccess: function(transport){
     	var json = transport.responseText.evalJSON();
			contacts_list_refresh(json);
   	}
	});
}

/*
 *
 *
 */
function contacts_init()
{
	contacts_load_all(0);
}

/*
 *
 *
 */
function contacts_form_init()
{
	$('contact_save').onclick = function(){
		form = $('contact_form');
		form.submit()
		}

	$('contact_cancel').onclick = function(){
		document.location ='index.php?c=ajax_contacts'; 
	}

	$('contact_delete').onclick = function(){
		if(confirm('Eliminar el usuario'))
		{
			new Ajax.Request('index.php?c=ajax_contacts&a=contact_delete&id=' + ITEM_ID, {
  			method:'get',
  			onSuccess: function(transport){
					document.location ='index.php?c=ajax_contacts'; 
				}
				});
		}
	}
}

/*
 *
 *
 */
function contact_init()
{
	contacts_form_init();
}

/*
 *
 *
 */
function conctact_mark_as_read(tr)		
{
	url = 'index.php?c=ajax_contacts&a=contact_mark&id=' + tr.name;
	new Ajax.Request(url, {
  	method:'get',
  	onSuccess: function(transport){
			var json = transport.responseText.evalJSON();
			display_success_message(json);
			var table = $('contacts_list');
			table.removeChild(tr);
   	}
	});
}

/*
 *
 *
 */
function contacts_list_refresh(items)
{
	var ul  = $('contacts_list');
	var lis =  $A(ul.childNodes);
	lis.each(function(item){
			ul.removeChild(item);
		});

	items.each(function(item){

    var at = document.createElement('textNode');
		at.innerHTML = '<b>' + item.id + '# ' + item.name + ' <a href="mailto:' + item.mail + '">'+item.mail+'</a></b>Fecha: ' + item.created_at + '<br>Contenido: ' +  item.content;

		var li= document.createElement('li');
		li.name = item.id;

		li.appendChild(at);
		li.onclick = function(){
			if(ITEM_FILTER == '3')
			{
				if(li.className == 'selected')
				{
					conctact_mark_as_read(li);		
				}
				else
				{
					li.className = 'selected';
					var uls = $('contacts_list');
					var lis =  $A(uls.childNodes);
					lis.each(function(item){
						if(li != item) item.className = '';
					});
				}
			}
		}

		ul.appendChild(li);
	})
}


